package com.ruoyi.web.controller.eduManage;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;

import com.ruoyi.eduManage.domain.EduMotivation;
import com.ruoyi.eduManage.service.IEduMotivationService;

/**
 * 激励语句管理Controller
 */
@RestController
@RequestMapping("/manage/motivation")
public class EduMotivationController extends BaseController {

    @Autowired
    private IEduMotivationService eduMotivationService;

    /** 列表查询 */
    @PreAuthorize("@ss.hasPermi('manage:motivation:list')")
    @GetMapping("/list")
    public TableDataInfo list(EduMotivation eduMotivation) {
        startPage();
        List<EduMotivation> list = eduMotivationService.selectEduMotivationList(eduMotivation);
        return getDataTable(list);
    }

    /** 导出 */
    @PreAuthorize("@ss.hasPermi('manage:motivation:export')")
    @Log(title = "激励语句管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EduMotivation eduMotivation) {
        List<EduMotivation> list = eduMotivationService.selectEduMotivationList(eduMotivation);
        ExcelUtil<EduMotivation> util = new ExcelUtil<>(EduMotivation.class);
        util.exportExcel(response, list, "激励语句数据");
    }

    /** 详情 */
    @PreAuthorize("@ss.hasPermi('manage:motivation:query')")
    @GetMapping(value = "/{motivationId}")
    public AjaxResult getInfo(@PathVariable("motivationId") Long motivationId) {
        return success(eduMotivationService.selectEduMotivationByMotivationId(motivationId));
    }

    /** 新增 */
    @PreAuthorize("@ss.hasPermi('manage:motivation:add')")
    @Log(title = "激励语句管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EduMotivation eduMotivation) {
        return toAjax(eduMotivationService.insertEduMotivation(eduMotivation));
    }

    /** 修改 */
    @PreAuthorize("@ss.hasPermi('manage:motivation:edit')")
    @Log(title = "激励语句管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EduMotivation eduMotivation) {
        return toAjax(eduMotivationService.updateEduMotivation(eduMotivation));
    }

    /** 删除 */
    @PreAuthorize("@ss.hasPermi('manage:motivation:remove')")
    @Log(title = "激励语句管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{motivationIds}")
    public AjaxResult remove(@PathVariable Long[] motivationIds) {
        return toAjax(eduMotivationService.deleteEduMotivationByMotivationIds(motivationIdsToStringArray(motivationIds)));
    }

    private String[] motivationIdsToStringArray(Long[] motivationIds) {
        String[] arr = new String[motivationIds.length];
        for (int i = 0; i < motivationIds.length; i++) {
            arr[i] = String.valueOf(motivationIds[i]);
        }
        return arr;
    }
}